package weaver.interfaces.xfjt.action;

import com.weaver.general.BaseBean;
import com.weaver.general.Util;
import weaver.conn.RecordSet;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-10-10
 * @Description: 4.流程结束，扣减对应物品库存数量
 * 5.流程结束，将扣减记录写入【办公用品入库台账】明细
 * @ModifyLog:
 **/
public class WeaBgypBflcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        writeLog(">>>>>WeaBgypBflcAction>>>>>requestid"+requestid);
        try {
            rs.executeQuery("select id,ffsj,sqr,sqrq from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            //发放时间
            String ffsj = Util.null2String(rs.getString("ffsj"));
            //申领人
            String sqr = Util.null2String(rs.getString("sqr"));
            //申领日期
            String sqrq = Util.null2String(rs.getString("sqrq"));

            rs.executeQuery("select mcll,mc,bfsl,lb from "+tablename+"_dt1 where mainid=?",mainid);

            while (rs.next()) {
                //名称
                String mcll = Util.null2String(rs.getString("mcll"));
                String mc = Util.null2String(rs.getString("mc"));
                //类别
                String lb = Util.null2String(rs.getString("lb"));
                //申领数量
                int bfsl = Util.getIntValue(rs.getString("bfsl"));
                int count;
                int xbfsl = bfsl;
                rs1.executeQuery("select id,ybfsl,dj,rksj,(sl-isnull(ylysl,0)-isnull(ybfsl,0)) sysl " +
                        "from uf_bgyprkmx where wpmc=? and wplb=? and (sfsc is null or sfsc=1) and (sl-isnull(ylysl,0)-isnull(ybfsl,0))>0 " +
                        " order by rksj", mcll,lb);
                while (rs1.next()) {
                    int id = Util.getIntValue(rs1.getString("id"));
                    int sysl = Util.getIntValue(rs1.getString("sysl"));
                    rs1.writeLog(">>>>id="+id+",sysl="+sysl+",bfsl="+bfsl);
                    //剩余数量
                    count = xbfsl - sysl;
                    if (count > 0) {
                        xbfsl = xbfsl - sysl;
                        rs1.executeUpdate("insert into uf_bgyprkmx_dt1(mainid,ffsj,slr,slrq,sllc,slsl) " +
                                "values (?,?,?,?,?,?)",id,ffsj,sqr,sqrq, requestid,sysl);
                        rs1.executeUpdate("update uf_bgyprkmx set ybfsl=isnull(ybfsl,0)+"+sysl+" where id=?",id);
                    } else {
                        rs1.executeUpdate("insert into uf_bgyprkmx_dt1(mainid,ffsj,slr,slrq,sllc,slsl) " +
                                "values (?,?,?,?,?,?)",id,ffsj,sqr,sqrq, requestid,xbfsl);
                        rs1.executeUpdate("update uf_bgyprkmx set ybfsl=isnull(ybfsl,0)+"+xbfsl+" where id=?",id);
                        break;
                    }

                }
                rs.executeUpdate("update uf_bgypkc set kc=kc-"+bfsl+" where id=?",mc);
            }
        return Action.SUCCESS;
    } catch(
    Exception e)

    {
        e.printStackTrace();
        requestInfo.getRequestManager().setMessageid("111111");
        requestInfo.getRequestManager().setMessagecontent("Action接口异常！");
        return Action.FAILURE_AND_CONTINUE;
    }
}

}
